//
//  Array+Extension.swift
//  LSTools
//
//  Created by Roffa Zhou on 2021/8/16.
//

import Foundation
public extension Array {
    
    /// 删除数组中元素的两种方式。 通过过滤与通过下标删除
    /// - Parameter item: 需要删除的对象
    /// - Returns: 删除后的新对象
    func removeItem(_ item: Element) -> Array where Element: Equatable{
        return self.filter {
            $0 != item
        }
    }
    mutating func remove(_ object: Element) where Element: Equatable{
        if let index = firstIndex(of: object) {
            remove(at: index)
        }
    }
    /// 数组转jsonString
    func jsonString() -> String {
        if !JSONSerialization.isValidJSONObject(self) {
            lsPrint("无法解析出JSONString")
            return ""
        }
        let data: NSData! = try? JSONSerialization.data(withJSONObject: self, options: []) as NSData?
        let JSONString = NSString(data: data as Data, encoding: String.Encoding.utf8.rawValue)
        return JSONString! as String
    }
}
